package com.app.micaihu.view.newsdetail.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.BaseListFragment;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.view.newsdetail.VideoDetailActivity;
import com.app.micaihu.view.newsdetail.adapter.VideoAboutAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 视频栏目剧集列表fragment
 * Created by linkai on 2016/9/6.
 */
public class VideoShowListFragment extends BaseListFragment<NewsEntity> {

    private String id;//剧集栏目id
    private String setNUm;//剧集id

    @Override
    protected void changeView() {
        mPageName = "VideoShowListFragment";
        Bundle args = getArguments();
        id = (args != null ? args.getString(Config.Extra.PARAMETER1, "") : "");
        setNUm = (args != null ? args.getString(Config.Extra.PARAMETER2, "") : "");
    }



    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String, String> parms = new HashMap<>();
        parms.put("page", dataPage + "");
        parms.put("id", id);
        parms.put("setNum", setNUm);
        HttpListener<DataBean<List<NewsEntity>>> httpListener = new HttpListener<DataBean<List<NewsEntity>>>() {
            @Override
            public void onStart() {
                setLoadView(LOADING,null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET,AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(DataBean<List<NewsEntity>> response) {

                if (response.noError() && response.getData() != null) {
                    List<NewsEntity> videolist = response.getData();
                    //数据去重
                    dataRevisited(videolist);
                    if (videolist != null && videolist.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<>();
                        }
                        if (isRefresh) {
                            dataList.clear();
                        }
                        dataList.addAll(videolist);
                        if (mAdapter == null) {
                            mAdapter = new VideoAboutAdapter(dataList, activity);
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES,"");
                    } else {
                        if(isRefresh){
                            setLoadView(DATAERROR,"");
                        }else{
                            setLoadView(DATAERROR,getString(R.string.pullup_nomore_data));
                        }
                    }
                } else {
                    setLoadView(DATAERROR,response.getNnderstoodMsg());
                }
            }
        };
        addPostCacheRequest(URIContant.VIDEO_SHOW_LIST, new TypeToken<DataBean<List<NewsEntity>>>() {
        }.getType(), parms, httpListener);
    }

    /**
     * 数据去重
     *
     * @param list
     */
    private void dataRevisited(List<NewsEntity> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size()) {
                break;
            }
            NewsEntity ne = list.get(i);
            if (ne == null) {
                continue;
            }
            if (!TypeConstant.getVideoIsPlay(ne)) {
                //如果视频不是规定的类型则移除
                list.remove(i);
                i--;
            }
        }
    }


    /**
     * 这是外部handler
     */
    private Handler handler;
    public void setHandler(Handler handler){
        this.handler = handler;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        //防止越界
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }
        NewsEntity news = dataList.get(position);
        if (news == null) {
            return;
        }
        if(handler != null){
            //如果外部操作不为空 则执行外部操作
            Message message = new Message();
            message.obj = news.getArticleId();
            handler.sendMessage(message);
            return;
        }
        Intent intent = new Intent(activity, VideoDetailActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1, news.getArticleId());
        intent.putExtra(Config.Extra.PARAMETER2, TypeConstant.SOURCETYPE_NORMAL);
        startActivity(intent);
    }

}
